﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;
public partial class System_User_List : PageBase
{
    private OrgainzationManager orgManager = new OrgainzationManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        this.txtHidOrg.Value = OrgHidValue;
        this.txtOrg.Text = OrgShowValue;
        if (!IsPostBack)
        {
            DataBind();
            InitForm();
        }
    }

    #region 绑定数据
    private void InitForm()
    {

        this.ddlPosition.DataSource = new DictionaryItemsManager().GetItemList(DictionaryEnum.UserPosition, MyStateManager.DomainID);
        this.ddlPosition.DataTextField = "NAME";
        this.ddlPosition.DataValueField = "DictionaryItemID";
        this.ddlPosition.DataBind();
        this.ddlPosition.Items.Insert(0, new ListItem("--所有--", ""));

        ddlGroup.DataSource = new GroupManager().LoadWithRecursion(MyStateManager.DomainID, MyStateManager.GroupID, MyStateManager.UserLevel, 1);
        ddlGroup.DataValueField = "ID";
        ddlGroup.DataTextField = "CODE";
        ddlGroup.DataBind();
        ddlGroup.Items.Insert(0, new ListItem("--所有--", "0"));

    }

    private void BindUser()
    {
        GridPager1.DataSource= UserManager.SelectUserDynamic(txtLoginName.Text,
                                                                txtName.Text,
                                                                ddlPosition.SelectedValue,
                                                                MyStateManager.DomainID,
                                                                txtHidOrg.Value,
                                                                this.ddlKind.SelectedValue,
                                                                decimal.Parse(ddlGroup.SelectedValue)).Tables[0];
       GridPager1.GridView = GridView1;
       GridPager1.BindData();
       GridPager1.Visible = GridView1.Rows.Count > 0;
    
         
    }
    #endregion

    #region 查询
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindUser();
    }
    #endregion

    #region 重置密码
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "reset")
        {
            UserManager manager = new UserManager();
            decimal userid = decimal.Parse(e.CommandArgument.ToString());

            //从配置表读取默认密码
            string defaultPwd = Jumpton.Common.Functions.EncryptString(new ConfigManager().GetSystemConfig(ConfigEnum.DEFAULT_PASSWORD, ConfigEnum.SUB_KEY001, MyStateManager.DomainID).Value);

          
            if (manager.ChangePassword(userid, Jumpton.Common.Functions.EncryptString(defaultPwd)))
                ShowMessage("密码重置成功");
            else
                ShowMessage("密码重置失败");

        }
    }
    #endregion

    public void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Grid_RowDataBound(sender, e);
            LinkButton btnReset = (LinkButton)e.Row.Cells[4].FindControl("btnReset");
            btnReset.Attributes.Add("onClick", "return window.confirm('你确定要重置密码吗？');");
        }
        this.Grid_RowDataBound(sender, e);
    }

    public void GridViewToolbarUserControl1_OnGVTBSetColumnsButtonEvent(object sender, EventArgs e)
    {
        BindUser();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindUser();
    }

    protected void btnExport_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format(@"..\ExportExcel\ExportExcel.aspx?Table=SystemUserList&LoginName={0}&Name={1}&Position={2}&Group={3}",
                                                                            txtLoginName.Text,
                                                                            txtName.Text,
                                                                            ddlPosition.SelectedValue,
                                                                            ddlGroup.SelectedValue));
    }
}
